#ifndef _LOGTHREAD_H
#define _LOGTHREAD_H

#include <cstdio>
#include <ctime>
#include <iostream>
#include <sstream>
#include <strstream>
#include "basethread.h"

using namespace std;
using namespace ThreadLib;

const int LOG_LENGTH = 1024;

typedef    FixedSizeType<LOG_LENGTH>    PRINTFTHREAD_ELEMENT_TYPE;

class LogThread :  public stringstream, public MessageThread<PRINTFTHREAD_ELEMENT_TYPE,100>
{
public:
    LogThread(void);
    virtual             ~LogThread(void);

    void                Run();

    void                lock();
    void                unLock();

    static ostream&     endl(ostream& os);
private:
    virtual void        createLog();
    virtual void        writeLogMessage(PRINTFTHREAD_ELEMENT_TYPE*);
    void                pushLogMessage();
protected:
    void                setMaxLog(int);
private:
    int                    m_printfCounter;
    int                    m_maxLog;
    FILE*                m_currentLog;
    ThreadLib::Mutex    m_mutex;
};

#define LOG( log_stream, content )        do{    \
    log_stream->lock();    \
{    \
    time_t theTime;    \
    time( &theTime );    \
    tm    t;    \
    localtime_s(&t, &theTime);    \
    (*log_stream) << "[" << t.tm_year+1900 << ":" << t.tm_mon+1 << ":" << t.tm_mday << ":" << t.tm_hour << ":" << t.tm_min <<":" << t.tm_sec << "]";    \
}    \
    (*log_stream) << content << LogThread::endl;    \
    log_stream->unLock();    \
}while(0)

#endif
